package controller

import (
	"github.com/labstack/echo"
	"github.com/sirupsen/logrus"
	"net/http"
	"src/config"
	"src/controller/param"
	"src/model"
	"src/util/context"
	"src/util/log"
)

var hotkeyLogger = log.GetLogger()

func writehotkeyLoggerLoggerLog(funcName, errMsg string, err error) {
	hotkeyLogger.WithFields(logrus.Fields{
		"package":  config.LogMessage,
		"file":     "hotkey.go",
		"function": funcName,
		"err":      err,
	}).Warn(errMsg)
}

func GetHotkey(c echo.Context) error {
	resData := map[string]interface{}{}
	var err error
	resData["hotkey"], err = model.GetHotKey()
	if err != nil {
		writehotkeyLoggerLoggerLog("GetHotkey", err.Error(), err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, resData)
}

func SetHotkey(c echo.Context) error {
	s := param.Hotkey{}
	err := c.Bind(&s)
	if err != nil || s.Key == "" {
		writehotkeyLoggerLoggerLog("SetHotkey", "param", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	err = model.AddHotMem(s.Key)
	if err != nil {
		writehotkeyLoggerLoggerLog("SetHotkey", "addHotkey", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, "")
}
